package enron;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import enron.Generator.Generator;

public class VendorAgentUI extends JFrame {
	private static final long serialVersionUID = -1347149286744960579L;
	private JPanel contentPane;
	private VendorAgent myAgent;
	private JLabel lblWattageValue;
	private JLabel lblEnergyValue;

	public VendorAgentUI(VendorAgent vendor) {
		this.myAgent = vendor;
		initGUI();
	}
	
	private void initGUI() {
		setTitle(myAgent.getLocalName());
		this.setBounds(100, 100, 300, 200);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.addWindowListener(new WindowListener() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				((VendorAgentUI)arg0.getComponent()).myAgent.doDelete();
				//.doDelete();
			}

			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setName("contentPane");
		contentPane.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.PREF_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
				new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("pref:grow"),}));
		{
			JLabel lblWattage = new JLabel("Wattage:");
			contentPane.add(lblWattage, "1, 1, right, default");
			lblWattage.setName("lblWattage");
		}
		{
			setLblWattageValue(new JLabel(myAgent.getPower().toString() + "W"));
			contentPane.add(getLblWattageValue(), "3, 1, left, default");
			getLblWattageValue().setName("lblWattageValue");
		}
		{
			JLabel lblEnergyUsage = new JLabel("Unsold:");
			contentPane.add(lblEnergyUsage, "1, 3, right, default");
			lblEnergyUsage.setName("lblEnergyUsage");
		}
		{
			setLblEnergyValue(new JLabel(VendorAgent.energyFormat.format(myAgent.getEnergyGenerated()*1000.0) + "Wh"));
			contentPane.add(getLblEnergyValue(), "3, 3, left, default");
			getLblEnergyValue().setName("lblEnergyValue");
		}
		{
			TitledBorder applianceBorder = new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Generators", TitledBorder.LEADING, TitledBorder.TOP, null, null);
			JPanel generatorPanelOuter = new JPanel();
			generatorPanelOuter.setName("generatorPanelOuter");
			contentPane.add(generatorPanelOuter, "1, 5, 3, 1, fill, fill");
			//generatorPanelOuter.setBorder(applianceBorder);
			generatorPanelOuter.setLayout(new BorderLayout(0, 0));
			{
				JScrollPane generatorScrollPane = new JScrollPane();
				generatorScrollPane.setBorder(applianceBorder);
				//generatorScrollPane.setViewportBorder();
				generatorScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
				generatorScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
				generatorScrollPane.setName("generatorScrollPane");
				generatorPanelOuter.add(generatorScrollPane);
				{
					JTable generatorTable = new JTable(); 
					generatorTable.setTableHeader(null);
					generatorScrollPane.setColumnHeaderView(null);
					generatorTable.setShowVerticalLines(false);
					generatorTable.setShowHorizontalLines(false);
					generatorTable.setShowGrid(false);
					generatorTable.setRowSelectionAllowed(false);
					generatorTable.setFillsViewportHeight(true);
					generatorTable.setBorder(new EmptyBorder(0, 0, 0, 0));
					generatorTable.setBackground(UIManager.getColor("Panel.background"));
					generatorTable.setModel(new DefaultTableModel(myAgent.getGeneratorArray(),new String[]{"Generators"}) {
						private static final long serialVersionUID = 5079746104881300480L;
						@SuppressWarnings({ "rawtypes" })
						Class[] columnTypes = new Class[] {
							Generator.class
						};
						@SuppressWarnings({ "unchecked", "rawtypes" })
						public Class getColumnClass(int columnIndex) {
							return columnTypes[columnIndex];
						}
					});
					generatorTable.getColumn("Generators").setCellRenderer(new GeneratorCellRenderer());
					generatorTable.getColumn("Generators").setCellEditor(new GeneratorCellEditor());
					generatorTable.setRowHeight(generatorTable.getColumn("Generators").getCellEditor().getTableCellEditorComponent(generatorTable, null, false, 0, 0).getMinimumSize().height);
					generatorTable.setMinimumSize(new Dimension(200, generatorTable.getColumn("Generators").getCellEditor().getTableCellEditorComponent(generatorTable, null, false, 0, 0).getMinimumSize().height*6));
					generatorTable.setPreferredScrollableViewportSize(generatorTable.getMinimumSize());
					generatorTable.setName("generatorTable");
					generatorScrollPane.setViewportView(generatorTable);
				}
			}
		}
		this.pack();
		this.setMinimumSize(this.getSize());
		this.setVisible(true);
	}

	public JLabel getLblEnergyValue() {
		return lblEnergyValue;
	}

	public void setLblEnergyValue(JLabel lblEnergyValue) {
		this.lblEnergyValue = lblEnergyValue;
	}

	public JLabel getLblWattageValue() {
		return lblWattageValue;
	}

	public void setLblWattageValue(JLabel lblWattageValue) {
		this.lblWattageValue = lblWattageValue;
	}

}
